package com.ah.shanxi_demo.sync.client;

import com.ah.shanxi_demo.common.enums.SXPortEnum;
import com.ah.shanxi_demo.entity.SxRegister;
import com.ah.http.httpsclient.MyHttpsClient;
import com.ah.http.httpsclient.constants.HttpHeaderContentTypeEnum;
import com.ah.shanxi_demo.sync.param.result.SyncPolicyResult;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

import static com.ah.shanxi_demo.sync.convert.ConvertSyncParam.convert_sync_policy;
import static com.ah.shanxi_demo.sync.convert.ConvertSyncRequest.convert_sync_request_header;
import static com.ah.shanxi_demo.sync.utils.syncResultConvertObject.sync_result_policy;

/**
 * @author zt 2020/8/21
 */
public class SyncPolicyClient {

    private static final Logger log = LoggerFactory.getLogger(SyncPolicyClient.class);

    /**
     * 部级平台 生成策略 客户端
     * @param sxRegister
     * @return
     */
    public static List<SyncPolicyResult> policy_client(SxRegister sxRegister) {

        //部级平台进行同步信息
        String policy_serviceAddress = sxRegister.getServiceAddress()+ SXPortEnum.POLICY.getKey();
        Object responseObject = sdk_policy(sxRegister, policy_serviceAddress);

        //解析返回值 返回同步表模型
        List<SyncPolicyResult> list = sync_result_policy(responseObject);

        return list;
    }

    /**
     * 生成策略接口_SDK
     *
     * @param param
     * @param sync_serviceAddress
     * @return
     */
    public static Object sdk_policy(SxRegister param, String sync_serviceAddress) {

        //地址
        String url = sync_serviceAddress;

        //参数
        Map<String, Object> paramsMap = convert_sync_policy(param);

        //请求头
        Map<String, String> headerMap = convert_sync_request_header(paramsMap);

        try {

            log.info("sync_policy【url】：" + JSON.toJSONString(url));
            log.info("sync_policy【paramsMap】：" + JSON.toJSONString(paramsMap));
            log.info("sync_policy【headerMap】：" + JSON.toJSONString(headerMap));

            Object responseObject = MyHttpsClient.sendPostHttps(url, paramsMap, headerMap, HttpHeaderContentTypeEnum.JSON.getKey());

            log.info("sync_policy【responseObject】：" + JSON.toJSONString(responseObject));
            return responseObject;
        } catch (Exception e) {
            log.error("sync_policy【Exception】：" + JSON.toJSONString(e));
        }
        return null;

    }
}
